package com.voxel.sense.infomsg.view.popwindows;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.example.com.common.utils.L;
import com.example.com.common.utils.SizeUtils;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.interfaces.listener.PopMenuItemClickCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenyiyao on 2017/10/31.
 */

public class PopWindowMenu extends PopupWindow {
    private View rootView;
    private ListView listview;
    private List<String> mDataList = new ArrayList<>();
    private Context mContext;
    private PopMenuItemClickCallBack callBack;
    private ArrayAdapter<String> mAdapter;

    public PopWindowMenu(Context context) {
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rootView = inflater.inflate(R.layout.item_popwindow_menu, null);
        listview = (ListView) rootView.findViewById(R.id.spinner_view);

        int h = SizeUtils.dp2px( 500);
        int w =SizeUtils.dp2px(  60);
        //设置SelectPicPopupWindow的视图
        this.setContentView(rootView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(w);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //刷新状态
        this.update();
        //实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(0000000000);
        //点后键和其他地方使其消失，设置了这个才能触发OnDismisslistener，设置其他控制变化等操作
//        this.setBackgroundDrawable(mContext.getResources().getDrawable(R.color.white));
        // mPopupWindow.setAnimationStyle（android.R.style.Animation_Dialog）;
        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.AnimationPreview);
        initMenu();
    }

    public void setData(List<String> list) {
        mDataList.clear();
        mDataList.addAll(list);
        mAdapter.notifyDataSetChanged();
        L.d("- mAdapter.getCount()" + mAdapter.getCount());
        mAdapter.getCount();
    }

    public boolean initMenu() {
        boolean flag = false;
        if (mDataList != null) {
            flag = true;
            mAdapter = new ArrayAdapter<String>(mContext, R.layout.item_spinner_area, mDataList);
            listview.setAdapter(mAdapter);
            listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (callBack != null) {
                        callBack.onPopItemClick(position);
                        dismiss();
                    }
                }
            });
        }
        return false;
    }

    public void setItemListener(PopMenuItemClickCallBack listener) {
        this.callBack = listener;
    }


}
